package com.cash.loan.modular.cash.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cash.loan.modular.cash.dao.ChannelUiMapper;
import com.cash.loan.modular.cash.model.ChannelUi;
import com.cash.loan.modular.cash.service.ChannelService;
import com.cash.loan.modular.cash.transfer.ChannelDto;
import com.cash.loan.modular.cash.transfer.ChannelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @Date 2018-08-01 13:47
 */
@Service
public class ChannerServiceImpl implements ChannelService {

    @Autowired
    private ChannelUiMapper channelUiMapper;

    @Override
    public List<ChannelVo> channelList(String keyword) {
        List<ChannelVo> channelList = channelUiMapper.getChannelList(keyword);
        return channelList;
    }

    @Override
    public Boolean checkChannelNameExist(String channelName) {
        int exist = channelUiMapper.existChannelName(channelName);
        if(exist >= 1) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean checkChannelCodeExist(String channelCode) {
        int exist = channelUiMapper.existChannelCode(channelCode);
        if(exist >= 1) {
            return true;
        } else {
            return false;
        }
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Boolean saveChannelInfo(ChannelDto channelDto) {
        ChannelUi channelUi = new ChannelUi();
        BeanUtils.copyProperties(channelDto,channelUi);
        channelUiMapper.insert(channelUi);
        return true;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Boolean changeUiType(Long id, Long uiType) {
        ChannelUi channelUi = new ChannelUi();
        channelUi.setId(id);
        channelUi.setUiType(uiType);
        channelUiMapper.updateById(channelUi);
        return true;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Boolean deleteById(Long id) {
        ChannelUi channelUi = new ChannelUi();
        channelUi.setId(id);
        channelUiMapper.delete(new EntityWrapper<>(channelUi));
        return true;
    }

    @Override
    public ChannelVo getDetailById(Long id) {
        return channelUiMapper.getDetailById(id);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Boolean updateChannel(ChannelDto channelDto) {
        ChannelUi channelUi = new ChannelUi();
        BeanUtils.copyProperties(channelDto,channelUi);
        channelUiMapper.updateAllColumnById(channelUi);
        return true;
    }
}
